import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class test2 {
    public static void main(String[] args) throws SQLException {
        //建立数据源
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java109?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("642364");

        //和数据库建立联系
        Connection connection= dataSource.getConnection();


        //构造sql
        String sql="select * from student";
        PreparedStatement statement=connection.prepareStatement(sql);

        //执行sql
        //表示查询的结果集合，后面就根据其进行遍历
        ResultSet resultSet=statement.executeQuery();


        //进行遍历
        while (resultSet.next()){
            int id=resultSet.getInt("id");
            String name=resultSet.getString("name");
            System.out.println("id="+id+"  "+"name="+name);
        }

        //释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
